﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using Framework;
using Newtonsoft.Json;

namespace FrameworkEditor
{
    internal class AllToOne : IAbSetting
    {
        internal AllToOne(string path, string regex) : base(path, regex)
        {

        }

        internal override List<AssetBundleBuild> GetAllBuilders()
        {
            List<AssetBundleBuild> ret = new List<AssetBundleBuild>();
            if (string.IsNullOrEmpty(DirPath))
            {
                return ret;
            }

            if (!Directory.Exists(DirPath))
            {
                Debug.LogWarning("AllToOne.GetAllBuilders, DirPath = " + DirPath + " is not exists!");
                return ret;
            }
            string abName = EditorUtils.GetRelativePathToDir(DirPath, EditorUtils.assetRootDir) + FrameworkSettings.AssetBundleExt;
            List<string> assetNames = new List<string>();

            foreach (string file in Directory.GetFiles(DirPath, "*.*", SearchOption.AllDirectories))
            {
                if (!IsNeedBuild(file))
                {
                    continue;
                }
                string assetPath = EditorUtils.GetPathRelativeToAssets(file);
                //Debug.Log("abName = " + abName + " assetPath = " + assetPath);
                assetNames.Add(assetPath);
            }
            //Debug.Log("abName = " + abName + " DirPath = " + DirPath + " EditorUtils.assetRootDir = " + EditorUtils.assetRootDir);
            var assetNameList = assetNames.ToArray();
            ret.Add(new AssetBundleBuild()
            {
                assetBundleName = abName,
                assetNames = assetNameList,
            });
            RecordAsset2BundleName(abName, assetNameList);
            return ret;
        }


        private static Dictionary<string, string> assetName2AbName;

        public static void FlushAssetName2AbNameDict(string platformName)
        {
            if (assetName2AbName==null)
            {
                return;
            }
            
            var jsonData = JsonConvert.SerializeObject(assetName2AbName, Formatting.Indented);
            var buildRootPath = Application.dataPath.Replace("Assets", FrameworkSettings.OutResLibName + "/" + platformName);
            FileHelper.WriteTextToPath(buildRootPath + "/configs/assetName2AbName.json", jsonData);
            var zipData = UnityZipUtil.ZipStr(jsonData);
            FileHelper.WriteAllBytesToPath(buildRootPath + "/configs/assetName2AbName.dat", zipData);
            assetName2AbName.Clear();
        }

        public static void StartRecord()
        {
            assetName2AbName = new Dictionary<string, string>();
            assetName2AbName.Clear();
        }
        
        static void RecordAsset2BundleName(string abName, string[] assetNameList)
        {
            foreach (string s in assetNameList)
            {
                assetName2AbName[s] = abName;
            }
        }
    }
}